﻿using Learun.Util;
using System.Data;
using Learun.Application.TwoDevelopment.ZZDT_EC;
using System.Web.Mvc;
using System.Collections.Generic;
using Learun.Util.SqlSugar;
using System.Linq;
using SqlSugar;
using Learun.Application.Base.SystemModule;
using System.Windows.Interop;

namespace Learun.Application.Web.Areas.ZZDT_EC.Controllers
{
    /// <summary>
    /// 版 本 PIT-ADMS V7.0.3 敏捷开发框架
    /// Copyright (c) 2013-2018 Hexagon PPM
    /// 创 建：超级管理员
    /// 日 期：2022-06-01 15:08
    /// 描 述：数据字典
    /// </summary>
    public class ec_dataitemController : MvcControllerBase
    {
        private ec_dataitemIBLL ec_dataitemIBLL = new ec_dataitemBLL();

        #region 视图功能

        /// <summary>
        /// 字典明细管理页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 字典明细管理表单页面
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Form()
        {
            return View();
        }

        /// <summary>
        /// 字典分类管理页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ClassifyIndex()
        {
            return View();
        }
        /// <summary>
        /// 字典分类管理表单页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult ClassifyForm()
        {
            return View();
        }

        #endregion

        #region 获取数据

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetPageList(string pagination, string queryJson)
        {
            Pagination paginationobj = pagination.ToObject<Pagination>();
            var data = ec_dataitemIBLL.GetList(queryJson, pagination: paginationobj);
            var jsonData = new
            {
                rows = data,
                total = paginationobj.total,
                page = paginationobj.page,
                records = paginationobj.records
            };
            return Success(jsonData);
        }

        /// <summary>
        /// 获取页面显示列表数据
        /// <summary>
        /// <param name="queryJson">查询参数</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetList(string queryJson)
        {
            var data = ec_dataitemIBLL.GetList(queryJson, false);
            return Success(data);
        }

        /// <summary>
        /// 获取表单数据
        /// <summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetFormData(string keyValue, string ProjectId)
        {
            var ec_dataitemData = ec_dataitemIBLL.GetEntity(keyValue, ProjectId);
            var ec_dataitemdetailData = ec_dataitemIBLL.GetEntity(ec_dataitemData.DataItemID, ProjectId);
            var jsonData = new
            {
                ec_dataitem = ec_dataitemData,
                ec_dataitemdetail = ec_dataitemdetailData,
            };
            return Success(jsonData);
        }
        /// <summary>
        /// 获取字典分类列表(树结构)
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetClassifyTree(string ProjectId)
        {
            var data = ec_dataitemIBLL.GetClassifyTree(ProjectId);
            return this.Success(data);
        }

        /// <summary>
        /// 分类编号不能重复
        /// </summary>
        /// <param name="DataItemCode">编码</param>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ExistItemCode(string keyValue, string DataItemCode, string ProjectId)
        {
            bool res = ec_dataitemIBLL.ExistItemCode(keyValue, DataItemCode, ProjectId);
            return Success(res);
        }
        /// <summary>
        /// 分类名称不能重复
        /// </summary>
        /// <param name="DataItemName">名称</param>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ExistItemName(string keyValue, string DataItemName, string ProjectId)
        {
            bool res = ec_dataitemIBLL.ExistItemName(keyValue, DataItemName, ProjectId);
            return Success(res);
        }

        #endregion

        #region 提交数据
        /// <summary>
        /// 暂时用smart 3d的数据导入，用于二三维校验用的part Name
        /// </summary>
        /// <param name="keyValue">事先上传到服务器的文件的id</param>
        /// <param name="ProjectId"></param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult ExcelImport(string keyValue, string ProjectId)
        {
            //MVC
            //查询文件的真实路径
            var file = new AnnexesFileBLL().GetByfolderIdEntity(keyValue);
            if (string.IsNullOrWhiteSpace(file?.F_FilePath))
            {
                return Fail("找不到文件" + file?.F_FilePath);
            }
            string strFilePath = file?.F_FilePath;
            var DataItemPartNumber= ec_dataitemIBLL.GetEntity("Part Number", ProjectId);
            if (DataItemPartNumber.IsEmpty())
            {
                return Fail("请先在分类树中建立一个节点为Part Number的节点");
            }
            string strPartNumberUID = DataItemPartNumber.DataItemID;
            ec_dataitemIBLL.PartNumberDetailImport(strPartNumberUID, strFilePath, ProjectId);
            return Success("导入成功！");
        }
        /// <summary>
        /// 删除实体数据
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteForm(string keyValue, string ProjectId)
        {
            ec_dataitemIBLL.DeleteEntity(keyValue, ProjectId);
            return Success("删除成功！");
        }
        /// <summary>
        /// 保存实体数据（新增、修改）
        /// <param name="keyValue">主键</param>
        /// <summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateAntiForgeryToken]
        [AjaxOnly]
        public ActionResult SaveForm(string keyValue, string strEntity, string ProjectId)
        {
            ec_dataitemEntity entity = strEntity.ToObject<ec_dataitemEntity>();
            ec_dataitemIBLL.SaveEntity(keyValue, entity, ProjectId);
            return Success("保存成功！");
        }
        #endregion

        #region 字典明细
        /// <summary>
        /// 获取数据字典明显根据分类编号
        /// </summary>
        /// <param name="dataItemCode">分类编号</param>
        /// <param name="keyword">查询条件</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDetailList(string dataItemCode, string keyword, string ProjectId)
        {
            var data = ec_dataitemIBLL.GetDetailList(dataItemCode, keyword, ProjectId);
            return Success(data);
        }

        /// <summary>
        /// 获取数据字典明显树形数据
        /// </summary>
        /// <param name="itemCode">分类编号</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetDetailTree(string itemCode, string ProjectId)
        {
            var data = ec_dataitemIBLL.GetDetailTree(itemCode, ProjectId);
            return Success(data);
        }
        /// <summary>
        /// 获取数据字典明细树形数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult GetTreeData(string ProjectId, bool isShowCheck, string DataItemID, string DataItemDetailID)
        {
            var tableName = ProjectSugar.TableName<ec_dataitemdetailEntity>(ProjectId);
            var list = SqlSugarHelper.Db.Queryable<ec_dataitemdetailEntity>().AS(tableName).Where(x => x.DataItemID == DataItemID).ToList();
            var childs = SqlSugarHelper.Db.Queryable<ec_dataitemdetailEntity>().AS(tableName).ToChildList(x => x.UpDataItemDetailID, DataItemDetailID);
            childs.ForEach(x =>
            {
                var item = list.FirstOrDefault(y => y.DataItemDetailID == x.DataItemDetailID);
                list.Remove(item);
            });
            List<TreeModel> treeList = new List<TreeModel>();
            foreach (var item in list)
            {
                TreeModel node = new TreeModel();
                node.id = item.DataItemDetailID;
                node.text = item.DataItemName;
                node.value = item.DataItemDetailID;
                node.showcheck = isShowCheck;
                node.checkstate = 0;
                node.isexpand = false;
                node.parentId = item.UpDataItemDetailID;
                node.NodeExtData = item;
                treeList.Add(node);
            }
            var res = treeList.ToTree();
            return Success(res);
        }
        /// <summary>
        /// 字典明细编号不能重复
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="DataItemCode">字典明细编号</param>
        /// <param name="itemCode">分类编码</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ExistDetailItemCode(string keyValue, string DataItemCode, string itemCode, string ProjectId)
        {
            bool res = ec_dataitemIBLL.ExistDetailItemCode(keyValue, DataItemCode, itemCode, ProjectId);
            return Success(res);
        }
        /// <summary>
        /// 字典明细名称不能重复
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="DataItemName">字典明细名称</param>
        /// <param name="itemCode">分类编码</param>
        /// <returns></returns>
        [HttpGet]
        [AjaxOnly]
        public ActionResult ExistDetailItemName(string keyValue, string DataItemName, string itemCode, string ProjectId)
        {
            bool res = ec_dataitemIBLL.ExistDetailItemName(keyValue, DataItemName, itemCode, ProjectId);
            return Success(res);
        }

        /// <summary>
        /// 保存明细数据实体
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <param name="itemCode">分类编码</param>
        /// <param name="entity">实体</param>
        [HttpPost]
        [AjaxOnly]
        [ValidateAntiForgeryToken]
        public ActionResult SaveDetailForm(string keyValue, string itemCode, string strEntity, string ProjectId)
        {
            ec_dataitemdetailEntity entity = strEntity.ToObject<ec_dataitemdetailEntity>();
            ec_dataitemIBLL.SaveDetailEntity(keyValue, entity, ProjectId);
            return Success("保存成功！");
        }

        /// <summary>
        /// 删除明细数据
        /// </summary>
        /// <param name="keyValue">主键</param>
        /// <returns></returns>
        [HttpPost]
        [AjaxOnly]
        public ActionResult DeleteDetailForm(string keyValue, string ProjectId)
        {
            ec_dataitemIBLL.DeleteDetail(keyValue, ProjectId);
            return Success("删除成功！");
        }

        #endregion
    }
}
